package com.normal.zbase.impl.tools.obj

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.core.view.size
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationBarView
import com.normal.zbase.R

/**
 * Created by zsf on 2023/3/29 11:34
 * ******************************************
 * *
 * ******************************************
 */
object NavigationBarViewObj {
    var listBadge = mutableListOf<TextView>()

    fun setBadgeValue(position: Int, value: String) {
        val textView = listBadge[position]
        textView.text = "$value"
    }

    fun initNavigationBar(bottomNavigationView: BottomNavigationView, context: Context) {
        bottomNavigationView.labelVisibilityMode = NavigationBarView.LABEL_VISIBILITY_LABELED
        val menuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView
        for (position in 0 until menuView.size) {
            val itemView = menuView.getChildAt(position) as BottomNavigationItemView
            val badgeView =
                LayoutInflater.from(context).inflate(R.layout.view_badge, menuView, false)
            itemView.addView(badgeView)
            val textView: TextView = badgeView.findViewById(R.id.sub_value)
            listBadge.add(textView)
            textView.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(
                    s: CharSequence?,
                    start: Int,
                    before: Int,
                    count: Int
                ) {
                    if (count == 0) {
                        textView.visibility = View.GONE
                    } else {
                        textView.visibility = View.VISIBLE
                    }
                }

                override fun afterTextChanged(s: Editable?) {
                }

            })
        }
    }
}